package com.allenyuan.gui;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import com.allenyuan.carvingsocketclient.AsyncSocketClient;
import com.allenyuan.carvingsocketclient.RemotingMethod;
import com.allenyuan.proto.TestPbProto.TestPb;
import com.google.protobuf.Message;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConnectionTestPanel extends AbstractPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7203848840112379201L;

	public ConnectionTestPanel(GuiContext context) {
		super(context);
		// nextButton.setEnabled(false);
		
		
		JButton testButton = new JButton("测试连接");
		testButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		testButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				RemotingMethod.TestPb(10, new AsyncSocketClient.ProgressListener() {
					
					@Override
					public void received(Message message) {
						// TODO Auto-generated method stub
						TestPb result = (TestPb) message;
						if (result.getTest() == 10) {
							JOptionPane.showMessageDialog(ConnectionTestPanel.this, "连接成功", "连接测试", JOptionPane.INFORMATION_MESSAGE);
							nextButton.setEnabled(true);
						}
					}
					
					@Override
					public void connected(boolean success) {
						// TODO Auto-generated method stub
						if (!success) {
							JOptionPane.showMessageDialog(ConnectionTestPanel.this, "连接失败", "连接测试", JOptionPane.ERROR_MESSAGE);
							nextButton.setEnabled(false);
						}
					}
				});
			}
		});
		optionPanel.add(testButton);
	}

	@Override
	protected String getGuideText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("首先进行本雕刻机的连接测试，请点击以下的按钮进行连接，若无法连接成功，请检查以下的参数：\n");
		sb.append("1、雕刻机是否开启电源\n");
		sb.append("2、雕刻机是否与计算机进行连接\n");
		sb.append("3、雕刻机是否与计算机处于同一网段");
		return sb.toString();
	}

	@Override
	protected String getGuideTitle() {
		// TODO Auto-generated method stub
		return "连接测试";
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals(nextButton.getActionCommand())) {
			((CardLayout)getParent().getLayout()).show(getParent(), ModeSelectPanel.class.getName());
		}
	}

}
